<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/2/28
 * 描述:
 */

namespace app\result\admin;

use app\model\KeFu;
use app\model\user\User;
use app\result\BaseResult;

class KeFuResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new KeFu();
    }

    public function save_data($args): array
    {
        $id = intval($args['kefu_id'] ?? 0);
        $user_id = intval($args['user_id'] ?? 0);
        if (!$user_id) {
            return $this->error_return('请选择关联用户');
        }
        $user = (new User())->data_get(['user_id' => $user_id]);
        if (!$user) {
            return $this->error_return('关联用户不存在');
        }
        $baseData = [
            'kefu_id' => $id,
            'status' => intval($args['status']),
            'remark' => $args['remark'] ?? '',
            'user_id' => $args['user_id'],
            'avatar' => $args['avatar'],
            'nickname' => $args['nickname'],
            'wechat' => $args['wechat'] ?? "",
            'mobile' => $args['mobile'] ?? '',
            'qq' => $args['qq'] ?? "",
            'email' => $args['email'] ?? "",
        ];
        if ($id) {
            $this->model->data_update(['kefu_id' => $id], $baseData);
        } else {
            $this->model->data_add($baseData);
        }
        return $this->success_return('保存成功');
    }

    // 表格列
    public function el_table_column()
    {
        return [
            ['prop' => 'kefu_id', 'label' => '客服编号', 'minWidth' => 100, 'width' => 80, 'align' => 'center',],
            ['prop' => 'nickname', 'label' => '昵称', 'minWidth' => 120, 'align' => 'center',],
            ['prop' => 'avatar', 'type' => 'image', 'label' => '头像', 'minWidth' => 100, 'align' => 'center',],
            ['prop' => 'status', 'type' => 'tags', 'label' => '状态', 'minWidth' => 100, 'align' => 'center',
                'tags' => [
                    1 => ['label' => '启用中', 'color' => 'white', 'type' => 'success'],
                    -1 => ['label' => '已禁用', 'color' => '', 'type' => 'danger'],
                ]
            ],
            ['prop' => 'username', 'label' => '关联用户', 'minWidth' => 120, 'align' => 'center',],
            ['prop' => 'wechat', 'label' => '微信号', 'minWidth' => 130, 'align' => 'center',],
            ['prop' => 'mobile', 'label' => '手机号', 'minWidth' => 130, 'align' => 'center',],
            ['prop' => 'qq', 'label' => 'QQ号', 'minWidth' => 130, 'align' => 'center',],
            ['prop' => 'email', 'label' => '邮箱', 'minWidth' => 130, 'align' => 'center',],
            ['prop' => 'remark', 'label' => '备注', 'minWidth' => 180, 'align' => 'center',],
            ['prop' => 'created_at', 'label' => '新增时间', 'minWidth' => 100, 'align' => 'center',],
            ['prop' => 'updated_at', 'label' => '更新时间', 'minWidth' => 100, 'align' => 'center',]

        ];
    }

    //列表搜索字段
    private function _searchField($args = [])
    {
        $data = [
            ['name' => 'keyword', 'label' => '关键词', 'type' => 'input', 'placeholder' => '搜索'],
            ['name' => 'user_id', 'label' => '用户ID', 'type' => 'input', 'placeholder' => '用户ID搜索'],
            ['name' => 'nickname', 'label' => '名称', 'type' => 'input', 'placeholder' => '名称搜索'],
        ];

        $data[] = ['name' => 'status', 'label' => '状态',
            'type' => 'select', 'filterable' => true,
            'options' => [
                ['label' => '全部', 'value' => ''],
                ['label' => '启用', 'value' => '1'],
                ['label' => '禁用', 'value' => '-1'],
            ]
        ];
        return $data;
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        $keyword = trim($args['keyword'] ?? '');
        $nickname = trim($args['nickname'] ?? '');
        $status = $args['status'] ?? '';
        $user_id = $args['user_id'] ?? '';
        if ($user_id) {
            $where[] = ['user_id', '=', $user_id];
        }
        if ($keyword) {
            $where[] = ['nickname|user_id', 'like', '%' . trim($args['keyword']) . '%'];
        }
        if (!empty($nickname)) {
            $where[] = ['nickname', '=', $nickname];
        }

        if ($status) {
            $where[] = ['status', '=', $status];
        }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'kefu_id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        if ($data) {
            $modelUser = new User();
            foreach ($data as &$v) {
                $v['username'] = $modelUser->get_value(['user_id' => $v['user_id']], 'username');
            }
            unset($v);
        }

        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField($args)
        ]);
    }

    public function get($args = []): array
    {
        $id = intval($args['id'] ?? 0);
        $data = $this->model->data_get(['kefu_id' => $id]);
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $data['username'] = (new User())->get_value(['user_id' => $data['user_id']], 'username');
        return $this->success_return('success', [
            'info' => $data,
            'el_table_column' => $this->el_table_column()
        ]);
    }


    public function set_status($args): array
    {
        $id = intval($args['id'] ?? 0);
        if (!$id) {
            return $this->error_return('参数错误');
        }
        $data = $this->model->data_get(['kefu_id' => $id], 'status,kefu_id');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $res = (new KeFu())->data_update(['kefu_id' => $id], ['status' => $data['status'] == 1 ? -1 : 1]);
        if (!$res) {
            return $this->error_return('修改客服状态失败');
        }
        admin_log('修改客服状态', $args);
        return $this->success_return('保存成功');
    }

    public function del_data($args): array
    {
        $ids = $args['ids'] ?? [];
        if (!$ids) {
            return $this->error_return('请选择数据');
        }
        $data = $this->model->data_get(['kefu_id' => $ids], '*');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        $res = $this->model->data_del([['kefu_id', 'in', $ids]]);
        if (!$res) {
            return $this->error_return('删除失败');
        }
        admin_log('删除客服数据', $data);
        return $this->success_return('删除成功');
    }
}
